﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Navigation : MonoBehaviour
{
    Vector2 start;//初始位置
    Vector2 quit;//目标位置
    Vector2 know;
    RectTransform parent;//自身的父级
    GameObject knowledge;
    // Start is called before the first frame update
    void Start()
    {
        knowledge = GameObject.FindGameObjectWithTag("Finish");
        know = knowledge.transform.position;
        parent = transform.GetComponent <RectTransform >();
        start = parent.position;
        quit = parent.parent.GetChild(0).transform.position;
        quit.y = 0;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    //知识页面移动
    public void KnowMove()
    {
        StartCoroutine(KnowPosition());
    }

    IEnumerator KnowPosition()
    {
        Vector2 target1;
        while (Mathf.Abs(knowledge.transform.position.x - start.x) > 10) 
        {
            target1 = Vector2.Lerp
                (knowledge.transform.position, start, 0.08f);
            target1.y = 367;
            knowledge.transform.position = target1;
            yield return new WaitForFixedUpdate();
        }
        EndKnow();
    }

    void EndKnow()
    {
        StopCoroutine(KnowPosition());
    }

    //移动到目标位置函数
    public void QuitMove()
    {
        StartCoroutine(QuitPosition());
    }

    IEnumerator QuitPosition()
    {
        Vector2 target2;
        while (Mathf.Abs(parent.position.x - quit.x) > 10)
        {
            target2 = Vector2.Lerp
                (parent.position, quit, 0.08f);
            target2.y = 367;
            parent.position = target2;
            yield return new WaitForFixedUpdate();
        }
        EndMove();
    }

    void EndMove()
    {
        print(quit);
        print(parent.position);
        StopCoroutine(QuitPosition());
    }

}
